#include "../include/client.h"

int check_cmd(char * token)
{
    int i=0;
    char buf[32];
    bzero(buf,sizeof(buf));
    for(;token[i]!='\0';i++)
    {
        buf[i]=toupper(token[i]);
    }
    if(strcmp(buf,"CD")==0) return CD;
    if(strcmp(buf,"LS")==0) return LS;
    if(strcmp(buf,"PUTS")==0) return PUTS;
    if(strcmp(buf,"GETS")==0) return GETS;
    if(strcmp(buf,"REMOVE")==0) return REMOVE;
    if(strcmp(buf,"PWD")==0) return PWD;
    if(strcmp(buf,"MKDIR")==0) return MKDIR;

    return DATA;
}

int pos_slash(char *line){
    char *first_char=strchr(line,'/');
    if(first_char!=NULL&&*(first_char-1)==' '){
        return 1;//cd /dir1
    }else if(first_char==NULL){
        return 2;//cd dir1
    }
    return 0;//cd dir1/dir2
}

int send_data(int sockfd,char *buf,char *token,int flag,int is_first){
    train_t train;
    while(1){
        bzero(buf,16);
        bzero(&train,sizeof(train));
        if(flag==0 || flag==2){
            token=strtok(NULL,"/\n");
            if(NULL==token){
                break;
            }
            train.type=DATA;
            train.len=strlen(token)+1;
            strcpy(train.data,token);
            send(sockfd,&train.type,sizeof(train.type),0);
            send(sockfd,&train.len,sizeof(train.len),0);
            send(sockfd,train.data,train.len,0);
            if(flag==2){
                break;
            }
            flag=1; 
        }
        token = strtok(NULL,"/\n");
        if(NULL==token)
        {   
            if(is_first){ //cd /
                train.type=DATA;
                train.len=2;
                strcpy(train.data,"/");
                send(sockfd,&train.type,sizeof(train.type),0);
                send(sockfd,&train.len,sizeof(train.len),0);
                send(sockfd,&train.data,train.len,0);
            }
            break;
        }
        is_first=0;
        buf[0]='/';
        strcpy(buf+1,token);

        train.type=check_cmd(token);
        if(train.type==DATA)
        {
            send(sockfd,&train.type,sizeof(train.type),0);
            train.len=strlen(buf);
            send(sockfd,&train.len,sizeof(train.len),0);
            strcpy(train.data,buf);
            send(sockfd,train.data,train.len,0);
        }
    }
    return 0;
}

int send_cmd(char *line,int sockfd)
{
    char filename[50]={0};
    char buf1[16]={0};
    int is_first=1;
    int flag=pos_slash(line);
    train_t train;
    bzero(&train,sizeof(train));
    char *token;
    token = strtok(line," /\n");
    int ret= check_cmd(token);
    train.type=ret;
    if(train.type == DATA)
    {
        printf("命令无效.\n");
        return -1;
    }
    send(sockfd,&train.type,sizeof(train.type),0);
    char buf[16];
    switch(train.type)
    {
    case CD:
        send_data(sockfd,buf,token,flag,is_first);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        break;
    case LS: 
        send_data(sockfd,buf,token,flag,is_first);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);      
        break;
    case PWD:
        send_data(sockfd,buf,token,flag,is_first);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        break;
    case MKDIR:
        send_data(sockfd,buf1,token,flag,is_first);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        break;
    case PUTS:
        bzero(filename,sizeof(filename));
        token = strtok(NULL," \n");
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        strcpy(filename,token);
        c_up(sockfd,filename);
        break;
    case GETS:
        bzero(filename,sizeof(filename));
        token = strtok(NULL," \n");
        strcpy(filename,token);
        ret = check_cmd(token);
        train.type=ret;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len = strlen(token);
        send(sockfd,&train.len,sizeof(train.len),0);
        strcpy(train.data,token);
        send(sockfd,train.data,train.len,0);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        c_recv_download(sockfd,filename);
        break;
    case REMOVE:
        send_data(sockfd,buf,token,flag,is_first);
        train.type=DATA;
        send(sockfd,&train.type,sizeof(train.type),0);
        train.len=0;
        send(sockfd,&train.len,sizeof(train.len),0);
        break;
    default:
        printf("无效命令！\n");
        break;
    }

    return 0;
}

